﻿#region Copyright (c) 2006-2012 nHydrate.org, All Rights Reserved
// --------------------------------------------------------------------- *
//                           NHYDRATE.ORG                                *
//              Copyright (c) 2006-2012 All Rights reserved              *
//                                                                       *
//                                                                       *
// This file and its contents are protected by United States and         *
// International copyright laws.  Unauthorized reproduction and/or       *
// distribution of all or any portion of the code contained herein       *
// is strictly prohibited and will result in severe civil and criminal   *
// penalties.  Any violations of this copyright will be prosecuted       *
// to the fullest extent possible under law.                             *
//                                                                       *
// THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED     *
// TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND         *
// TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
// CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY     *
// THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF THE NHYDRATE GROUP *
//                                                                       *
// UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN     *
// PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR  *
// SUBSTANTIALLY THE SAME, FUNCTIONALITY AS THIS PRODUCT                 *
//                                                                       *
// THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE           *
// CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF NHYDRATE,          *
// THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO             *
// INSURE ITS CONFIDENTIALITY.                                           *
//                                                                       *
// THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT        *
// PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN       *
// EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING         *
// THE PRODUCT.  IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE            *
// SOURCE CODE CONTAINED HEREIN.                                         *
//                                                                       *
// THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE.              *
// --------------------------------------------------------------------- *
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using nHydrate.Dsl;

namespace nHydrate.DslPackage.Forms
{
	public partial class EntityAuditMappings : Form
	{
		private nHydrate.Dsl.nHydrateModel _model = null;
		private Dictionary<Entity, TreeNode> _nodeCache = new Dictionary<Entity, TreeNode>();
		private bool _isLoading = false;

		public EntityAuditMappings()
		{
			InitializeComponent();
		}

		public EntityAuditMappings(nHydrate.Dsl.nHydrateModel model)
			: this()
		{
			_model = model;

			#region Load Tree
			_isLoading = true;
			foreach (var item in model.Entities.OrderBy(x => x.Name))
			{
				var n = new TreeNode() { Text = item.Name, Tag = item };
				tvwItem.Nodes.Add(n);
				_nodeCache.Add(item, n);

				n.Nodes.Add(new TreeNode() { Text = "Create Audit", Tag = "C", Checked = item.AllowCreateAudit });
				n.Nodes.Add(new TreeNode() { Text = "Modify Audit", Tag = "M", Checked = item.AllowModifyAudit });
				n.Nodes.Add(new TreeNode() { Text = "Concurrency", Tag = "T", Checked = item.AllowTimestamp });
				n.Checked = item.AllowCreateAudit || item.AllowModifyAudit || item.AllowTimestamp;
			}
			_isLoading = false;

			#endregion
		}

		private bool _isChecking = false;
		private void tvwItem_AfterCheck(object sender, TreeViewEventArgs e)
		{
			if (_isChecking) return;
			_isChecking = true;
			try
			{
				if (!_isLoading)
				{
					//IF check/uncheck an entity then do the same for all its fields
					if (e.Node.Tag is Entity)
					{
						e.Node.Nodes.ToList<TreeNode>().ForEach(x => x.Checked = e.Node.Checked);
					}

					if (e.Node.Tag is string)
					{
						//If check a field then make sure its parent entity is checked too
						if (e.Node.Checked)
						{
							e.Node.Parent.Checked = true;
						}
						else
						{
							//If NO fields for an entity are checked then uncheck parent entity
							if (e.Node.Parent.Nodes.ToList<TreeNode>().Count(x => x.Checked) == 0)
							{
								e.Node.Parent.Checked = false;
							}
						}
					}
				}

			}
			catch (Exception ex)
			{
				throw;
			}
			finally
			{
				_isChecking = false;
			}
		}

		private void cmdCheck_Click(object sender, EventArgs e)
		{
			_nodeCache.Values.ToList().ForEach(x => x.Checked = true);
		}

		private void cmdUncheck_Click(object sender, EventArgs e)
		{
			_nodeCache.Values.ToList().ForEach(x => x.Checked = false);
		}

		private void cmdExpand_Click(object sender, EventArgs e)
		{
			_nodeCache.Values.ToList().ForEach(x => x.Expand());
		}

		private void cmdCollapse_Click(object sender, EventArgs e)
		{
			_nodeCache.Values.ToList().ForEach(x => x.Collapse());
		}

		private void cmdOK_Click(object sender, EventArgs e)
		{
			try
			{
				using (var transaction = _model.Store.TransactionManager.BeginTransaction(Guid.NewGuid().ToString()))
				{
					foreach (var item in _nodeCache)
					{
						item.Key.AllowCreateAudit = item.Value.Nodes[0].Checked;
						item.Key.AllowModifyAudit = item.Value.Nodes[1].Checked;
						item.Key.AllowTimestamp = item.Value.Nodes[2].Checked;
					}
					transaction.Commit();
				}

			}
			catch (Exception ex)
			{
				throw;
			}

			this.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.Close();
		}

		private void cmdCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.Close();
		}

	}
}
